import requests
from lxml import etree
import re
import xlwt

data = []
j=0

headers ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.57',
'referer': 'https://www.runoob.com/html/html-tutorial.html',
'cookie':'SERVERID=fb669a01438a4693a180d7ad8d474adb|1616560842|1616560033'
}

# 将HTML文件导出，使用ETREE初始化
url = "https://www.runoob.com/html/html-tutorial.html"
data = requests.get(url,headers).text
html = etree.HTML(data)
# html = etree.parse(wb_data, etree.HTMLParser())
result_title = html.xpath('//*[@id="leftcolumn"]/a/text()')
result_href = html.xpath('//*[@id="leftcolumn"]/a/@href')
print(result_title, result_href)

# 写入xls
work = xlwt.Workbook(encoding = 'utf-8')
worksheet = work.add_sheet('runoob')
for i in result_title:
    j=j+1
    worksheet.write(j+1,0,label = i)
    worksheet.write(j+1,1,label = result_href[j-1])
work.save('./runoob.xls')